#include "rgb2cmyk.h"

float max3(float a, float b, float c) {
    float max = a;
    if (b > max) max = b;
    if (c > max) max = c;
    return max;
}

void rgb_to_cmyk(RGB input[HEIGHT][WIDTH], CMYK output[HEIGHT][WIDTH]) {
    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            float r = (float)input[i][j].r / 255.0f;
            float g = (float)input[i][j].g / 255.0f;
            float b = (float)input[i][j].b / 255.0f;

            float k = 1.0f - max3(r, g, b);

            float c = 0.0f;
            float m = 0.0f;
            float y = 0.0f;

            if (k < 1.0f) {
                float denom = 1.0f - k;
                c = (1.0f - r - k) / denom;
                m = (1.0f - g - k) / denom;
                y = (1.0f - b - k) / denom;
            }

            output[i][j].c = c;
            output[i][j].m = m;
            output[i][j].y = y;
            output[i][j].k = k;
        }
    }
}

